class SessionController < ApplicationController

  def create
    redirect_to home_path and return if logged_in?
    user = User.authenticate(params[:name], params[:password])
    unless user.nil?
      session[:user_id] = user.id
      cookies[:rorbb_remember_me] = {:value => "#{user.id}:#{user.reset_login_token!}", :expires => Time.now.utc+1.year} if params[:remember_me]
      user.seen_now!
      flash[:notice] = "Welcome #{current_user.name}"
      redirect_to home_path
    else
      flash[:error] = 'Username or password is incorrect'
      redirect_to new_session_path
    end
  end
  
  def destroy
    reset_session
    cookies.delete :rorbb_remember_me
    flash[:notice] = "You are now logged out"
    redirect_to new_session_path
  end

end
